﻿using System;
using System.Web.UI;
using System.Data.SqlClient;

namespace SoftwareEngineering_Project
{
    public partial class Booking : System.Web.UI.Page
    {

        private SoftwareEngineering_Project.User CurrentUser
        {
            get
            {
                return (SoftwareEngineering_Project.User)Session["CurrentUser"];
            }
            set
            {
                Session["CurrentUser"] = value;
            }
        }
        
        
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                //Populate the reservation dropdown menu
                Reservation myReservation = new Reservation();
                SqlDataReader myreader = myReservation.getReservation("Select * from Reservation_Type");

                ConferenceType.DataSource = myreader;
                ConferenceType.DataTextField = "Descr";
                ConferenceType.DataValueField = "TypeID";
                ConferenceType.DataBind();
                myReservation.CloseConnection();

                //Get the Audio and Video rooms

                string sqlexactroom;

                if (ConferenceType.SelectedValue == "1")//room Only
                {
                    sqlexactroom = "Select * from Room ";
                }
                else if (ConferenceType.SelectedValue == "2")//Video 
                {
                    sqlexactroom = "Select * from Room where Video=1";
                }
                else if (ConferenceType.SelectedValue == "3")//Audio
                {
                    sqlexactroom = "Select * from Room where Audio=1";
                }
                else
                {
                    sqlexactroom = "Select * from Room where Audio=1 AND Video=1";
                }

                //Populate the reservation dropdown menu
                Room myRoom = new Room();
                SqlDataReader myreader2 = myRoom.getRooms(sqlexactroom);
                Location.DataSource = myreader2;
                Location.DataTextField = "Name";
                Location.DataValueField = "Room_ID";
                Location.DataBind();

            }
        }

        protected void Save_Click(object sender, EventArgs e)
        {
           
            Reservation myreservation = new Reservation(Convert.ToInt32(Location.SelectedValue), title.Text, Organizer.Text, 
               Convert.ToDateTime(date.Text).Date,Convert.ToDateTime(starttime.Text), Convert.ToDateTime(endtime.Text), 0,Convert.ToInt32(ConferenceType.SelectedValue),
                AudioYes.Checked, VideoYes.Checked, PresentationYes.Checked, CurrentUser.Username  );
            string sqlcommand = "Select * from reservation where Room_ID ='" + Convert.ToInt32(Location.SelectedValue) + "' and Res_date='" + Convert.ToDateTime(date.Text).Date.ToString("yyyy-MM-dd") +"' and STime<='" + Convert.ToDateTime(endtime.Text) + "' and Etime>='" + Convert.ToDateTime(starttime.Text) + "' ";
            SqlDataReader sqlreader = myreservation.getReservation(sqlcommand);
            if (sqlreader.HasRows)//check if the event overlap with another event
            {
                Errormessage.Visible = true;
                Errormessage.Text = " Please choose a different date or a different conference room";
            }
            else
            {

                if (myreservation.AddtoDataBase() == true)
                {
                    Errormessage.Visible = true;
                    Errormessage.Text = " Reservation was added successfully!";
                }
                else
                {
                    Errormessage.Visible = true;
                    Errormessage.Text = " Database Error";
                }

            }

            
        }

        protected void endtime_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Organizer.Text = Location.SelectedValue + "-" + Location.SelectedItem + " _ " + Location.SelectedIndex; 
        }
    }
}